<template>
	<view class="address-edit">
		<view class="form bg-white">
			<u-field v-model="addressObj.user_name" label="联系人" placeholder="请填写收货人姓名">
			</u-field>
			<u-field v-model="addressObj.mobile" label="联系方式" placeholder="请填写手机号码">
			</u-field>
			<index-send :isFast="false" ref="indexSend"></index-send>
		</view>

		<button class="my-btn bg-primary white br60" @tap="formSubmit">完成</button>
	</view>
</template>

<script>
	// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | gitee下载：https://gitee.com/likeshop_gitee
// | github下载：https://github.com/likeshop-github
// | 访问官网：https://www.likeshop.cn
// | 访问社区：https://home.likeshop.cn
// | 访问手册：http://doc.likeshop.cn
// | 微信公众号：likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用，未经许可不能去除前后端官方版权标识
// |  likeshop系列产品收费版本务必购买商业授权，购买去版权授权后，方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的，任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
	import {
		editAddress,
		getOneAddress,
		hasRegionCode,
		addAddress
	} from '@/api/user';
	import area from '@/utils/area'
	export default {
		data() {
			return {
				addressObj: {
					user_name: '',
					mobile: '',
				},
				region: '',
				addressId: '',
				defaultRegion: ['广东省', '广州市', '番禺区'],
				defaultRegionCode: '440113',
				showRegion: false,
				lists: []
			};
		},
		onLoad: function(options) {
			this.addressId = parseInt(options.id)
			if (options.id) {
				uni.setNavigationBarTitle({
					title: '编辑地址'
				});
				this.getOneAddressFun();
			} else {
				uni.setNavigationBarTitle({
					title: '添加地址'
				});
				this.getWxAddressFun();
			}
			this.$nextTick(() => {
				this.lists = area
			})
		},

		onUnload: function() {
			uni.removeStorageSync('wxAddress');
		},

		methods: {
			formSubmit() {
				let {user_name,mobile} = this.addressObj;
				
				if(user_name==''){
					this.$toast({title:'请输入联系人'})
					return
				}
				if(mobile==''){
					this.$toast({title:'请输入联系方式'})
					return
				}
				console.log(this.$refs.indexSend);
				let indexSend = this.$refs.indexSend;
				let addressList =  indexSend.addressList.concat();
				var transport_address = [];
				console.log(addressList)
				// 筛选空数组		
				addressList.forEach((item,index)=>{
					if(item.name==null && item.is_start != 1){
						addressList.splice(index,1);
						return 
					}
					if(index==addressList.length-1){
						return 
					}				
					if(addressList[index+1].name==null){
						return
					}
					transport_address.push( {
						start_latitude:addressList[index].latitude,
						start_longitude:addressList[index].longitude,
						start_address:addressList[index].name,
						start_province:addressList[index].province,
						start_city:addressList[index].city,
						start_district:addressList[index].district,
						end_latitude:addressList[index+1].latitude,
						end_longitude:addressList[index+1].longitude,
						end_address:addressList[index+1].name,
						end_province:addressList[index+1].province,
						end_city:addressList[index+1].city,
						end_district:addressList[index+1].district,
						type:addressList.length-2==index?1:2
					})
				})
				
				
				
				console.log(transport_address)
				if(transport_address.length==0){
					this.$toast({title:'请选择正确地址'})
					return
				}
				
				addAddress({
					user_name,mobile,
					transport_address:JSON.stringify(transport_address)
				}).then(res=>{
					if(res.code==1){
						this.$toast({
							title:'添加成功'
						})
						setTimeout(()=>{
							uni.navigateBack({
								
							})
						},1500)
						
					}
				})			
			},
			regionChange(region) {
				this.addressObj.province_id = region[0].value;
				this.addressObj.city_id = region[1].value;
				this.addressObj.district_id = region[2].value;
				this.region = region[0].label + " " + region[1].label + " " + region[2].label
			},

			getOneAddressFun() {
				getOneAddress(this.addressId).then(res => {
					if (res.code == 1) {
						let {
							city,
							province,
							district
						} = res.data;
						this.addressObj = res.data;
						this.region = `${province} ${city} ${district}`
					}
				});
			},

			getWxAddressFun() {
				let wxAddress = uni.getStorageSync('wxAddress');
				if (!wxAddress) return;
				wxAddress = JSON.parse(wxAddress)
				let {
					userName: contact,
					telNumber: telephone,
					provinceName: province,
					cityName: city,
					detailInfo: address
				} = wxAddress;
				let district = wxAddress.countryName || wxAddress.countyName
				hasRegionCode({
					province,
					city,
					district
				}).then(res => {
					if (res.code == 1) {
						if (res.data.province) {
							this.region = `${province} ${city} ${district}`;
							this.addressObj.contact = contact;
							this.addressObj.telephone = telephone
							this.addressObj.address = address
							this.addressObj.province_id = res.data.province;
							this.addressObj.city_id = res.data.city;
							this.addressObj.district_id = res.data.district;
						}
					}
				});
			}

		}
	};
</script>
<style lang="scss">

	.address-edit {
		padding-top: 10rpx;


		.my-btn {
			margin: 30rpx 26rpx;
			text-align: center;
		}
	}

</style>
